<template>
  <a-row :gutter="24">
    <a-col :md="4" :sm="24">
      <a-card :bordered="false" :loading="treeLoading">
        <a-directory-tree
          :showIcon="false"
          :treeData="treeList"
          v-if="treeList.length"
          @select="handleClick"
          :defaultExpandAll="true"
          :replaceFields="replaceFields"/>
      </a-card>
    </a-col>
    <a-col :md="19" :sm="24">
      <a-card :bordered="false">

        <div class="table-operator">
          <a-button
            v-if="isShow(stationId)"
            class="btn_add"
            type="primary"
            @click="$refs.addForm.add(stationId)"
            icon="plus">新增
          </a-button>
        </div>

        <s-table
          ref="table"
          size="default"
          :columns="columns"
          :data="loadData"
          :alert="true"
          :rowKey="(record) => record.id"
        >

          <span slot="timeSchedule" slot-scope="text">
            {{ getTime(text) }}
          </span>
          <span slot="action" slot-scope="text, record">
            <a @click="$refs.editForm.edit(record)" class="btn_edit">编辑</a>
            <a-divider type="vertical"/>
            <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => del(record)">
              <a class="btn_del">删除</a>
            </a-popconfirm>
          </span>
        </s-table>

        <add-form ref="addForm" @ok="handleOk" :addTitle="addTitle" />
        <edit-form ref="editForm" @ok="handleOk" :edittitle="addTitle" />

      </a-card>
    </a-col>
  </a-row>
</template>

<script>
  import { STable } from '@/components'
  import { page, del } from '@/api/modular/main/bizgatewayschedule/bizGatewayScheduleManage'
  import addForm from './addForm'
  import editForm from './editForm'
  import ARow from 'ant-design-vue/es/grid/Row'
  import { treeList } from '@/api/modular/main/bizstation/bizStationManage'
  import { gatewayList } from '@/api/modular/main/bizgateway/bizGatewayManage'

  export default {
    components: {
      ARow,
      STable,
      addForm,
      editForm
    },

    data () {
      return {
      addTitle: '',
        advanced: false, // 高级搜索 展开/关闭
        queryParam: {}, // 查询参数
        columns: [ // 表头
          {
            title: '录音时段',
            dataIndex: 'timeSchedule',
            scopedSlots: { customRender: 'timeSchedule' }
          },
          {
            title: '网关名称',
            dataIndex: 'gatewayName',
            width: 100
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: 150,
            scopedSlots: { customRender: 'action' },
            align: 'center'
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return page(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        defaultExpandedKeys: [],
        replaceFields: {
          key: 'id'
        },
        treeList: [],
        treeLoading: true,
        gatewayList: [],
        gatewayIds: [],
        stationId: ''
      }
    },

    created () {
      this.getStationTree()
    },

    methods: {
      del (record) {
        del(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      getStationTree () {
        treeList(Object.assign(this.queryParam)).then(res => {
          this.treeLoading = false
          if (!res.success) {
            return
          }
          this.treeList = res.data
        })
      },
      handleClick (e) {
        var returnedItem = '';


this.treeList.forEach((item) => {

  if (item.id == e[0]) {


    returnedItem = item;
    return item;
  }
  else if (item.children.length > 0) {

    if (item.children.id = e[0]) {
      returnedItem = item.children
      return item.children
    }
  }
})


returnedItem.forEach((item) => {

  if (item.id == returnedItem.id) {
    this.addTitle = item.title
  }
})
        if (e.length > 0) {
          this.queryParam = {}
          const param = {
            stationId: e.toString()
          }
          this.stationId = e.toString()
          this.queryParam.gatewayIds = ''
          gatewayList(param).then(res => {
            this.gatewayIds = []
            this.gatewayList = res.data
            for (let i = 0; i < res.data.length; i++) {
              this.gatewayIds.push(res.data[i].id)
            }
            this.queryParam.gatewayIds = this.gatewayIds.toString()
            this.$refs.table.refresh()
          })
        } else {
          this.queryParam.gatewayIds = ''
          this.$refs.table.refresh()
        }
      },
      isShow () {
        return !!this.stationId
      },
      getTime (text) {
        var time = text.split('')
        var times = ''
        for (let i = 0; i < time.length; i++) {
          if (time[i] === '1') {
            times += [i] + '时，'
          }
        }
        var endTime = times.slice(0, times.length - 1)
        if (endTime.length === 0) {
          return '未录音'
        }
        return times.slice(0, times.length - 1)
      }
    }
  }
</script>

<style lang="less" scoped>
  @import '../~@/assets/css/list.less';

  .table-operator {
    margin-bottom: 18px;
    padding-right: 10px;
    /deep/ .btn_add{
      float: right;
    }
  }

  button {
    margin-right: 8px;
  }
</style>
